package org.spring.blog.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Entity
@Table(name = "tb_article")
public class Article {

    @Id
    private String id;

    @Column(name = "title")
    private String title;

    @Column(name = "user_id")
    private String userId;

    @Column(name = "user_avatar")
    private String userAvatar;

    @Column(name = "user_name")
    private String userName;

    @Column(name = "category_id")
    private String categoryId;

    @Column(name = "content")
    private String content;

    @Column(name = "type")
    private String type;

    @Column(name = "state")
    private String state;

    @Column(name = "summary")
    private String summary;

    @Column(name = "labels")
    private String label;

    @Column(name = "view_count")
    private long viewCount;

    @Column(name = "create_time")
    private Date createTime;

    @Column(name = "update_time")
    private Date updateTime;

    @Column(name = "cover")
    private String cover;

    @OneToOne(targetEntity = UserNoPassword.class)
    @JoinColumn(name = "user_id", referencedColumnName = "id", insertable = false, updatable = false)
    private UserNoPassword user;

    // 忽略这个属性的注释
    @Transient
    private List<String> labels = new ArrayList<>();


    public String getLabel() {

        labels.clear();

        if (label == null) {
            return null;
        }

        if (label.contains("-")) {
            String[] split = label.split("-");
            List<String> strings = Arrays.asList(split);

            labels.addAll(strings);
        } else {
            labels.add(label);
        }

        return label;
    }
}
